package com.zwk.cms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwk.base.entity.CommonResult;
import com.zwk.cms.entity.CrmBanner;
import com.zwk.cms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author zwk
 * @since 2022-08-27
 */
@RestController
@RequestMapping("/cms/banner/admin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    // 分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("/page/{current}")
    public CommonResult getPageList(@PathVariable("current") Integer current){
        //创建page对象
        Page<CrmBanner> pageTeacher = new Page<>(current,5);
        IPage<CrmBanner> page = crmBannerService.page(pageTeacher, null);

        // 判断current是否合法
        if(current > page.getPages()){
            current = Math.toIntExact(page.getPages());
            pageTeacher = new Page<>(current,5);
            page = crmBannerService.page(pageTeacher, null);
        }

        long total = page.getTotal();
        List<CrmBanner> records = page.getRecords();

        return CommonResult.ok().data("total", total).data("records", records);

    }

}

